﻿package ;

#if flash
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.LineScaleMode;
import flash.text.TextField;
import flash.utils.ByteArray;
import homponex.core.BitmapS9G;
import homponex.core.Button;
import homponex.utils.Grid;
#end
import heox.Rectangle;

import homponex.core.Component;
import homponex.core.Panel;

import heox.events.PositionEvent;
import heox.events.DimensionEvent;
//import homponex.utils.Grid;

import heox.Point;

/**
 * ...
 * @author alijaya
 */

class Main 
{
	
	static function main()
	{
		#if flash9
		init2();
		#end
	}
	
	#if flash9
	static function init6()
	{
		var s:Sprite = new Skin_Panel();
		var bd:BitmapData = new BitmapData(Math.ceil(s.width), Math.ceil(s.height), true, 0x00000000);
		bd.draw(s);
		//flash.Lib.current.addChild(new Bitmap(bd));
		var b:ByteArray = bd.getPixels(bd.rect);
		b.position = 0;
		var tf:TextField = new TextField();
		tf.text += "[ ";
		for (n in 0...Std.int(b.length / 4))
		{
			tf.text += Std.string(b.readInt()) + ", ";
		}
		tf.text += "];";
		flash.Lib.current.addChild(tf);
	}
	
	static function init5()
	{
		var p0:Profile = { sX: 5.0, sY: 5.0, t: 0.05, c: 0xeeeeee };
		var p1:Profile = { sX: 10.0, sY: 10.0, t: 0.1, c: 0xcccccc };
		var p2:Profile = { sX: 25.0, sY: 25.0, t: 0.5, c: 0x888888 };
		var p3:Profile = { sX: 50.0, sY: 50.0, t: 1.0, c: 0x333333 };
		var p4:Profile = { sX: 100.0, sY: 100.0, t: 1.5, c: 0x000000 };
		var grid:Grid = new Grid(300, 300, [p0, p1, p2, p3, p4]);
		flash.Lib.current.addChild(grid);
		
		//var bd:BitmapData = checker(10, 0xff0000, 0xffff00);
		var rect:Rectangle = new Rectangle();
		rect.top = 2;
		rect.left = 2;
		rect.bottom = 14;
		rect.right = 14;
		var s9g1:BitmapS9G = new BitmapS9G(Skin_Button_Up, rect);
		s9g1.setPosDim(100, 100, 100, 100);
		var s9g2:BitmapS9G = new BitmapS9G(Skin_Button_Over, rect);
		s9g2.setPosDim(150, 150, 100, 100);
		var s9g3:BitmapS9G = new BitmapS9G(Skin_Button_Down, rect);
		s9g3.setPosDim(175, 125, 100, 100);
		flash.Lib.current.addChild(s9g1.view);
		flash.Lib.current.addChild(s9g2.view);
		flash.Lib.current.addChild(s9g3.view);
	}
	#end
	
	static function checker(s:Int, c1:UInt, c2:UInt) : BitmapData
	{
		var bd:BitmapData = new BitmapData(s, s);
		for (j in 0...s)
		{
			for (i in 0...s)
			{
				var c:UInt = ((j + i) % 2 == 0)? c1 : c2;
				bd.setPixel(i, j, c);
			}
		}
		return bd;
	}
	
	static function init4()
	{
		var p:Point = new Point(100, 100);
		var r:Rectangle = new Rectangle(100, 100, 100, 100);
		r.addEventListener(DimensionEvent.WIDTH_AND_HEIGHT, function(e:DimensionEvent) { trace(e.target); } );
		r.setDim(200, 50);
		trace(p);
		p.add(new Point(10, 10));
		trace(p);
		//trace(r);
	}
	
	static function init3()
	{
		var t:TestEventDispatcher = new TestEventDispatcher("theunga");
		t.addEventListener(TestEvent.CHANGE_STRING, function(e:TestEvent) { trace(e.string); } );
		t.changeString("tauge");
		t.changeString("kecambah");
		t.changeString("dll");
	}
	
	static function init2()
	{
		var c:Panel = new Panel();
		var c1:Panel = new Panel();
		var c2:Panel = new Panel();
		var c3:Panel = new Panel();
		var c4:Panel = new Panel();
		var c21:Button = new Button();
		
		c.setPosDim(50, 50, 500, 500);
		c1.setPercentPosDim(true, true, true, true);
		c1.setPPosDim(0, 0, 1, 0.1);
		c2.setPercentPosDim(true, true, true, true);
		c2.setPPosDim(0, 0.1, 0.25, 0.9);
		c3.setPercentPosDim(true, true, true, true);
		c3.setPPosDim(0.25, 0.1, 0.75, 0.45);
		c4.setPercentPosDim(true, true, true, true);
		c4.setPPosDim(0.25, 0.55, 0.75, 0.45);
		c21.setPosDim(25, 25, 50, 50);
		
		c.addChild(c1);
		c.addChild(c2);
		c.addChild(c3);
		c.addChild(c4);
		c2.addChild(c21);
		
		
		flash.Lib.current.addChild(c.view);
		
		trace(c.getTree());
	}
	
	/*static function init1() 
	{
		var p0:Profile = { sX: 5.0, sY: 5.0, t: 0.05, c: 0xeeeeee };
		var p1:Profile = { sX: 10.0, sY: 10.0, t: 0.1, c: 0xcccccc };
		var p2:Profile = { sX: 25.0, sY: 25.0, t: 0.5, c: 0x888888 };
		var p3:Profile = { sX: 50.0, sY: 50.0, t: 1.0, c: 0x333333 };
		var p4:Profile = { sX: 100.0, sY: 100.0, t: 1.5, c: 0x000000 };
		var grid:Grid = new Grid(300, 300, [p0, p1, p2, p3, p4]);
		Lib.current.addChild(grid);
		var s:Sprite = makeGraphic();
		s.width = 200;
		s.height = 200;
		Lib.current.addChild(s);
	}
	
	static function makeGraphic() : Sprite
	{
		var s:Sprite = new Sprite();
		s.graphics.beginFill(0xffff00, 0.5);
		s.graphics.drawRoundRect(0, 0, 100, 100, 50, 50);
		s.scale9Grid = new Rectangle(25, 25, 50, 50);
		return s;
	}*/
	
}

import hevex.EventDispatcher;
class TestEventDispatcher extends EventDispatcher
{
	var string:String;
	
	public function new(s:String)
	{
		super();
		changeString(s);
	}
	
	public function changeString(s:String)
	{
		string = s;
		dispatchEvent(new TestEvent(TestEvent.CHANGE_STRING, string));
	}
}

import hevex.Event;
class TestEvent extends Event
{
	public static var CHANGE_STRING:String = "CHANGE_STRING";
	
	public var string(default, null) : String;
	
	public function new(type:String, string:String)
	{
		super(type);
		this.string = string;
	}
}

#if flash9
class Skin_Panel extends flash.display.Sprite
{
	public function new()
	{
		super();
	}
}

class Skin_Button_Up extends flash.display.Sprite
{
	public function new()
	{
		super();
	}
}

class Skin_Button_Over extends flash.display.Sprite
{
	public function new()
	{
		super();
	}
}

class Skin_Button_Down extends flash.display.Sprite
{
	public function new()
	{
		super();
	}
}

class Skin_Button_Disabled extends flash.display.Sprite
{
	public function new()
	{
		super();
	}
}
#end